package com.bidevalution.dao.entity;

import com.bidevalution.dao.enums.CheckJobParamsEnum;
import com.bidevalution.dao.enums.CheckMsgTypeEnum;
import com.google.common.base.Splitter;
import lombok.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@RequiredArgsConstructor
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class CheckJob {
    @NonNull
    private String checkJobClz;
    @NonNull
    private String sectionId;
    @NonNull
    private String sectionStepId;
    @NonNull
    private String submitCode;
    @NonNull
    private String expertId;
    @NonNull
    private String layoutCode;
    @NonNull
    private CheckMsgTypeEnum checkMsgType;
    private String enterpriseId;

    // 全部专家
    private List<BeSectionExpertInfo> allExperts;
    // 扩展参数
    private Map<String, String> params;

    public static List<CheckJob> parseCheckJob(String checkJobClass) {
        List<CheckJob> list = new ArrayList<>();
        String[] jobs = checkJobClass.split("\\r");

        for (String jobStr: jobs) {
            CheckJob checkJob = new CheckJob();
            String[] strs = jobStr.split("\\?");
            // 获取job
            checkJob.setCheckJobClz(strs[0]);
            // 解析参数
            checkJob.params = strs.length >1? Splitter.on("&").withKeyValueSeparator("=").split(strs[1]) : new HashMap<>();

            list.add(checkJob);
        }
        return list;
    }

    public void init(String sectionId, String sectionStepId, String submitCode, String expertId, String layoutCode, CheckMsgTypeEnum checkMsgType){
        this.sectionId = sectionId;
        this.sectionStepId = sectionStepId;
        this.submitCode = submitCode;
        this.expertId = expertId;
        this.layoutCode = layoutCode;
        this.checkMsgType = checkMsgType;
    }

    /**
     * 获取参数值
     * @param key
     * @return
     */
    public String getParam(CheckJobParamsEnum key) {
        return params.get(key.name);
    }

}
